import {UserModel} from "../biz/model/UserModel";

export class Token {

    static isLogged() {
        /**
         * 后面实现cookie保存登陆状态
         */
        return localStorage.getItem("login") !== "false";
    }

    static getAuthorization(): string | null {
        return localStorage.getItem("login");
    }

    static login(username: string, password: string, user: UserModel) {
        localStorage.setItem("login", "true");
        let btoa1 = window.btoa(username+":"+password);
        let authorization = "Basic " + btoa1;
        this.setAuthorization(authorization);
        localStorage.setItem("user", JSON.stringify(user));
        return this.isLogged();
    }

    static logout() {
        localStorage.setItem("login", "false");
        localStorage.removeItem("user");
        return this.isLogged();
    }

    static user(): UserModel | null {
        let item = localStorage.getItem("user");
        if (item === null) {
            return null;
        }
        return JSON.parse(item);
    }

    static setAuthorization(authorization: string) {
        localStorage.setItem("login", authorization);
    }
}
